package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;

    /*
    * 添加购物车
    * */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        /*
        * 判断购物车中是否有该菜品或者是套餐
        * */
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        // 如果购物车中有该菜品或者套餐则数量加一
        ShoppingCart sc = shoppingCartMapper.list(shoppingCart);
        if(sc != null){
            sc.setNumber(sc.getNumber() + 1);
            shoppingCartMapper.updateNumberById(sc);
        }else {
            // 否则将该菜品或者套餐添加到购物车中
            Long dishId = shoppingCart.getDishId();
            Long setmealId = shoppingCart.getSetmealId();
            if (setmealId != null) {
                // 在套餐表中根据id查询名字和图片
                SetmealVO setmealVO = setmealMapper.selectSetmealById(setmealId);
                shoppingCart.setSetmealId(setmealId);
                shoppingCart.setName(setmealVO.getName());
                shoppingCart.setImage(setmealVO.getImage());
                shoppingCart.setAmount(setmealVO.getPrice());
            }else {
                DishVO dishVO = dishMapper.selectById(dishId);
                shoppingCart.setDishId(dishId);
                shoppingCart.setName(dishVO.getName());
                shoppingCart.setImage(dishVO.getImage());
                shoppingCart.setAmount(dishVO.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /*
    * 查看购物车中的商品
    * */
    @Override
    public List<ShoppingCart> getShoppingCartList() {
        Long UserId = BaseContext.getCurrentId();
        return shoppingCartMapper.selectAllByUserId(UserId);
    }

    /**
     * 购物车的商品数量减一
     * @param shoppingCartDTO
     * @return void
     * @author xinggang
     * @create 2025/2/25
     **/
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 查询数据库中该商品是否有多个
        ShoppingCart sc = shoppingCartMapper.list(shoppingCart);
        Integer number = sc.getNumber();
        if (number == 1) {
            // 如果数量等于1，则将该商品删除
            shoppingCartMapper.deleteByIdOrFlavor(sc);
        }else if (number > 1) {
            // 如果数量大于一则将该商品的数量减一
            sc.setNumber(number - 1);
            shoppingCartMapper.updateNumberById(sc);
        }
    }

    /**
     * 清空购物车
     * @return void
     * @author xinggang
     * @create 2025/2/25
     **/
    @Override
    public void clean() {
        shoppingCartMapper.deleteAll();
    }
}


